package com.controller;

import com.aop.MultiTransaction;
import com.auth.FrontUserIsLoginAuth;
import com.bug.mamage.WordInfoManage;
import com.model.ExplainDetail;
import com.model.FrontUser;
import com.model.WordInfo;
import com.model.WordTable;
import com.model.form.BaseTableForm;
import com.model.pojo.UserLinkWord;
import com.model.response.Response;
import com.model.vo.Datas;
import com.model.vo.Word;
import com.model.vo.WordCard;
import com.pepository.neo4j.WordRelationRepository;
import com.service.ExplainDetailService;
import com.service.WordInfoService;
import com.service.WordTableService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.websocket.EncodeException;
import java.io.IOException;



@RestController
@RequestMapping("/word")
public class WordController extends BaseController{

    @Autowired
    private WordInfoManage wordInfoManage;
    @Autowired
    private WordInfoService wordInfoService;
    @Autowired
    private WordTableService wordTableService;
    @Autowired
    private WordRelationRepository wordRelationRepository;
    @Autowired
    private ExplainDetailService explainDetailService;

    @FrontUserIsLoginAuth
    @PostMapping("/hasWord")
    public Response hasWord(String word) {
        if(Strings.isEmpty(word)) return Response.fail("word not null!");
        boolean result = wordInfoService.hasWord(word);
        return Response.success(result);
    }

    @FrontUserIsLoginAuth
    @MultiTransaction
    @PostMapping("/addWord")
    public Object addWord(String word) throws IOException, EncodeException {
        if(Strings.isEmpty(word)) return Response.fail("word not null!");
        boolean result = wordInfoService.hasWord(word);
        FrontUser user = getFrontUser();
        if(result) {
            // 已经入库, 添加neo4j标签, 返回完整数据
            WordInfo wordInfo = wordInfoService.findByWord(word);
            if(!wordTableService.hasByUserIdAndWord(user.getId(), word)) {
                UserLinkWord link = new UserLinkWord()
                        .setWord(word).setWordId(wordInfo.getId()).setUserId(user.getId());
                wordInfoService.addNeo4jWordTable(link);
                WordTable wordTable = wordInfoService.addDbWordTable(link);
                wordInfoService.flashViewSocket(user.getId(), Word.create(wordInfo, wordTable));
            }else {
                return Response.success().setMsg("has_before").putKV("dbId", wordInfo.getId());
            }
        } else{
            // 新单词, 需要爬取收据入库, 返回只返回word字段, 由前端再做请求处理
            wordInfoManage.catchWord(word);
            // 记录要关联的用户
            wordInfoService.addLinkUser(user.getId(), word);
        }
        return Response.success().setMsg("add_word");
    }

    @FrontUserIsLoginAuth
    @PostMapping("/list")
    public Response<Page> list(@Valid BaseTableForm form) {
        Page result = wordInfoService.queryUserPage(getFrontUser(), form);
        return Response.success(result);
    }

    @FrontUserIsLoginAuth
    @PostMapping("/mapData")
    public Response mapData() {
        Datas datas = wordInfoService.getDatas(getFrontUser().getId());
        return Response.success(datas);
    }

    @FrontUserIsLoginAuth
    @PostMapping("/getWordCard")
    public Response getWordCard(Long wordId) {
        if(wordId == null) return Response.fail("传参错误");
        // 组装单词卡对象, 理解, 各种词意, 关联的以后单词
        WordCard result = wordInfoService.getWordCardByWordInfoId(getFrontUser().getId(), wordId);
        return Response.success(result);
    }

    @FrontUserIsLoginAuth
    @PostMapping("/getTypeText")
    public Response getTypeText(Long typeId) {
        if(typeId == null) return Response.fail("传参错误");

        return Response.success();
    }

    @FrontUserIsLoginAuth
    @PostMapping("/getExplainDetail")
    public Response getDetailText(Long detailId) {
        if(detailId == null) return Response.fail("传参错误");
        ExplainDetail explained = explainDetailService.getById(detailId);
        return Response.success(explained);
    }
}
